Completed
Push — master ( 3f43c1...51db49 )
by Maxence
03:26
created

FullTextSearch.onSearchReset   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
rs 9.4285
cc 1
nc 1
nop 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A 0 3 1
1
/*
2
 * Files_FullTextSearch - Index the content of your files
3
 *
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later. See the COPYING file.
6
 *
7
 * @author Maxence Lange <[email protected]>
8
 * @copyright 2018
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
27
/** global: OCA */
28
29
var fullTextSearch = OCA.FullTextSearch.api;
30
31
32
var elements = {
33
	old_files: null,
34
	search_result: null,
35
	current_dir: ''
36
};
37
38
39
var FullTextSearch = function () {
40
	this.init();
41
};
42
43
44
FullTextSearch.prototype = {
45
46
	/**
47
	 * File actions handler, defaults to OCA.Files.FileActions
48
	 * @type OCA.Files.FileActions
49
	 */
50
	fileActions: null,
51
52
53
	init: function () {
54
		var self = this;
55
56
		elements.old_files = $('#app-content-files');
57
58
		elements.search_result = $('<div>');
59
		elements.search_result.insertBefore(elements.old_files);
60
61
		fullTextSearch.setResultContainer(elements.search_result);
62
		fullTextSearch.setEntryTemplate(self.generateEntryTemplate());
63
		fullTextSearch.setResultHeader(self.generateResultHeader());
64
		// fullTextSearch.setResultFooter(self.generateResultFooter());
65
66
		fullTextSearch.initFullTextSearch('files', 'files', self);
67
68
		this._initFileActions();
69
	},
70
71
	_initFileActions: function () {
72
73
		this.fileActions = new OCA.Files.FileActions();
74
		this.fileActions.registerDefaultActions();
75
		this.fileActions.merge(window.FileActions);
76
		this.fileActions.merge(OCA.Files.fileActions)
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
77
78
		this._onActionsUpdated = _.bind(this._onActionsUpdated, this);
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
79
		OCA.Files.fileActions.on('setDefault.app-files', this._onActionsUpdated);
80
		OCA.Files.fileActions.on('registerAction.app-files', this._onActionsUpdated);
81
		window.FileActions.on('setDefault.app-files', this._onActionsUpdated);
82
		window.FileActions.on('registerAction.app-files', this._onActionsUpdated);
83
84
		// if (this._detailsView) {
85
		// 	this.fileActions.registerAction({
86
		// 		name: 'Details',
87
		// 		displayName: t('files', 'Details'),
88
		// 		mime: 'all',
89
		// 		order: -50,
90
		// 		iconClass: 'icon-details',
91
		// 		permissions: OC.PERMISSION_NONE,
92
		// 		actionHandler: function (fileName, context) {
93
		// 			self._updateDetailsView(fileName);
94
		// 		}
95
		// 	});
96
		// }
97
	},
98
99
100
	_onActionsUpdated: function (ev, newAction) {
0 ignored issues
show
Unused Code introduced by
The parameter newAction is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
101
		if (ev.action) {
102
			this.fileActions.registerAction(ev.action);
103
		} else if (ev.defaultAction) {
104
			this.fileActions.setDefault(
105
				ev.defaultAction.mime,
106
				ev.defaultAction.name
107
			);
108
		}
109
	},
110
111
112
	generateResultHeader: function () {
113
114
		var resultHeader = $('<div>', {class: 'files_header'});
115
		resultHeader.append($('<div>', {class: 'files_div_checkbox'}).html('&nbsp;'));
116
		resultHeader.append($('<div>', {class: 'files_div_thumb'}).html('&nbsp;'));
117
		resultHeader.append(
118
			$('<div>', {class: 'files_header_div files_div_name'}).text(_('Name')));
119
		resultHeader.append(
120
			$('<div>', {class: 'files_header_div files_div_modified'}).text(_('Modified')));
121
		resultHeader.append(
122
			$('<div>', {class: 'files_header_div files_div_size'}).text(_('Size')));
123
124
		return resultHeader;
125
	},
126
127
128
	generateResultFooter: function () {
129
		var resultFooter = $('<div>', {class: 'files_footer'});
130
131
		return resultFooter;
132
	},
133
134
135
	/**
136
	 *
137
	 * !!! use this in the fulltextsearch app
138
	 * !!! use this in the fulltextsearch app
139
	 * !!! use this in the fulltextsearch app
140
	 */
141
	generateEntryTemplate: function () {
142
143
		var resultName = $('<div>', {class: 'files_result_file'});
144
		resultName.append($('<div>', {
145
			id: 'title',
146
			class: 'files_result_title'
147
		}));
148
		resultName.append($('<div>', {
149
			id: 'extract',
150
			class: 'files_result_extract'
151
		}));
152
153
		var resultEntry = $('<div>', {class: 'files_result'});
154
		resultEntry.append($('<div>', {class: 'files_div_checkbox'}));
155
		resultEntry.append($('<div>', {class: 'files_div_thumb files_result_div'}));
156
157
		resultEntry.append($('<div>', {class: 'files_result_div files_div_name'}).append(resultName));
158
		resultEntry.append(
159
			$('<div>', {class: 'files_result_div files_result_item files_div_size'}));
160
		resultEntry.append(
161
			$('<div>', {class: 'files_result_div files_result_item files_div_modified'}));
162
163
		return $('<div>').append(resultEntry);
164
	},
165
166
167
	onEntryGenerated: function (divEntry, entry) {
168
169
		var divFile = divEntry.find('.files_result');
170
		divFile.attr({
171
			'data-id': entry.id,
172
			'data-type': entry.info.type,
173
			'data-size': entry.info.size,
174
			'data-file': entry.info.file,
175
			'data-mime': entry.info.mime,
176
			'data-mtime': entry.info.mtime,
177
			'data-etag': entry.info.etag,
178
			'data-permissions': entry.info.permissions,
179
			'data-path': entry.info.path
180
		});
181
182
		var mtime = parseInt(entry.info.mtime, 10) * 1000;
183
		var size = OC.Util.humanFileSize(parseInt(entry.info.size, 10), true);
0 ignored issues
show
Bug introduced by
The variable OC seems to be never declared. If this is a global, consider adding a /** global: OC */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
184
		var thumb = '/index.php/core/preview?fileId=' + entry.id + '&x=32&y=32&forceIcon=0&c=' +
185
			entry.info.etag;
186
		divEntry.find('.files_div_size').text(size);
187
		divEntry.find('.files_div_modified').text(OC.Util.relativeModifiedDate(mtime));
188
		divEntry.find('.files_div_thumb').css('background-image', 'url("' + thumb + '")');
189
	},
190
191
192
	onEntrySelect: function (divEntry, event) {
193
194
		var resultEntry = divEntry.find('.files_result');
195
		this.fileActions.currentFile = resultEntry;
196
197
		var path = resultEntry.attr('data-path');
198
		var filename = resultEntry.attr('data-file');
199
		var mime = resultEntry.attr('data-mime');
200
		var type = resultEntry.attr('data-type');
201
		var permissions = resultEntry.attr('data-permissions');
202
203
		if (type !== 'file') {
204
			return false;
205
		}
206
207
		if (event && (event.ctrlKey || event.which === 2 || event.button === 4)) {
208
			return false;
209
		}
210
		// 	window.open('/remote.php/webdav' + path + '/' + filename);
211
		// } else {
212
		// 	window.open('/remote.php/webdav' + path + '/' + filename, '_self');
213
		// }
214
215
216
		var action = this.fileActions.getDefault(mime, type, permissions);
217
218
		if (action) {
219
220
			event.preventDefault();
221
			window.FileActions.currentFile = this.fileActions.currentFile;
222
			action(filename, {
223
				$file: resultEntry,
224
				fileList: this,
225
				fileActions: this.fileActions,
226
				dir: path
227
			});
228
229
			return true;
230
		}
231
232
		return false;
233
		// if (event && (event.ctrlKey || event.which === 2 || event.button === 4)) {
234
		// 	window.open('/remote.php/webdav' + path + '/' + filename);
235
		// } else {
236
		// 	window.open('/remote.php/webdav' + path + '/' + filename, '_self');
237
		// }
238
239
240
	},
241
242
243
	getModelForFile: function () {
244
		return null;
245
	},
246
247
248
	changeDirectory: function (targetDir, changeUrl, force, fileId) {
249
		var self = this;
250
		var currentDir = '/';
251
		targetDir = targetDir || '/';
252
		if (!force && currentDir === targetDir) {
253
			return;
254
		}
255
		this._setCurrentDir(targetDir, changeUrl, fileId);
256
257
		// discard finished uploads list, we'll get it through a regular reload
258
		this._uploads = {};
259
		this.reload().then(function (success) {
260
			if (!success) {
261
				self.changeDirectory(currentDir, true);
262
			}
263
		});
264
	},
265
266
	onSearchRequest: function (data) {
267
		if (data.options.files_within_dir === '1') {
268
			var url = new URL(window.location.href);
0 ignored issues
show
Bug introduced by
The variable URL seems to be never declared. If this is a global, consider adding a /** global: URL */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
269
			data.options.files_within_dir = url.searchParams.get("dir");
270
		}
271
	},
272
273
274
	onResultDisplayed: function () {
275
		elements.old_files.fadeOut(150, function () {
276
			elements.search_result.fadeIn(150);
277
		});
278
	},
279
280
281
	onResultClose: function () {
282
		elements.search_result.fadeOut(150, function () {
283
			elements.old_files.fadeIn(150);
284
		});
285
	},
286
287
288
	onSearchReset: function () {
289
		elements.search_result.fadeOut(150, function () {
290
			elements.old_files.fadeIn(150);
291
		});
292
	}
293
294
};
295
296
297
OCA.Files.FullTextSearch = FullTextSearch;
298
299
$(document).ready(function () {
300
	OCA.Files.FullTextSearch = new FullTextSearch();
301
});
302
303
304
305